package com.gome.wechat.program.common.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by liwei113 on 2017/10/19 0019.
 */
public class DateUtil {

    private static SimpleDateFormat sdfYMD = new SimpleDateFormat("yyyy-MM-dd");
    private static SimpleDateFormat sdfYYYYMMDD = new SimpleDateFormat("yyyyMMdd");
    private static SimpleDateFormat sdfGangYYMMDD = new SimpleDateFormat("yyyy/MM/dd");
    private static SimpleDateFormat sdfYYYYMM = new SimpleDateFormat("yyyyMM");
    private static SimpleDateFormat sdfYYMM = new SimpleDateFormat("yyMM");
    private static SimpleDateFormat sdfYYYYGangMM = new SimpleDateFormat("yyyy-MM");
    private static SimpleDateFormat sdfYMDHMS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


    private static Calendar calendar = Calendar.getInstance();
    public static String formatYYMM(Date date){
        if(null == date){
            return null;
        }
        return sdfYYMM.format(date);
    }

    public static String formatYYYYMMDD(Date date){
        if(null == date){
            return null;
        }
        return sdfYYYYMMDD.format(date);
    }

    public static String formatYMD(Date date){
        if(null == date){
            return null;
        }
        return sdfYMD.format(date);
    }

    public static String formatYYYYGangMM(Date date){
        if(null == date){
            return null;
        }
        return sdfYYYYGangMM.format(date);
    }

    public static String formatYMDHMS(Date date){
        if(null == date){
            return null;
        }
        return sdfYMDHMS.format(date);
    }


    public static Date parseYMDHMS(String dateStr){
        if(null == dateStr){
            return null;
        }
        Date date = null;
        try {
            date =  sdfYMDHMS.parse(dateStr);
        }catch (Exception e){
            return null;
        }
        return date;
    }

    public static Date plusMonth(Date date,int amount){
        if(null == date){
            return null;
        }
        calendar.setTime(date);
        calendar.add(Calendar.MONTH,amount);
        return  calendar.getTime();
    }

    public static Date plusDate(Date date,int amount){
        if(null == date){
            return null;
        }
        calendar.setTime(date);
        calendar.add(Calendar.DATE,amount);
        return  calendar.getTime();
    }

    /**
     * 获取当前时间的1号凌晨0点
     * @param date
     * @return
     */
    public static Date getFirstDate(Date date){
        if(null == date){
            return null;
        }
        calendar.setTime(date);
        calendar.set(Calendar.DATE,1);
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        return calendar.getTime();
    }

    /**
     * 获取当前时间最后一天得23.59.59
     * @param date
     * @return
     */
    public static Date getLastDate(Date date){
        if(null == date){
            return null;
        }
        calendar.setTime(date);
        calendar.add(Calendar.MONTH,1);
        calendar.set(Calendar.DATE,1);
        calendar.add(Calendar.DATE,-1);
        calendar.set(Calendar.HOUR_OF_DAY,23);
        calendar.set(Calendar.MINUTE,59);
        calendar.set(Calendar.SECOND,59);
        return calendar.getTime();
    }

    /**
     * 获取月份差值
     * @param date1
     * @param date2
     * @return
     */
    public static Integer getMonthSpace(Date date1, Date date2) {
        if(null == date1 || null == date2){
            return null;
        }
        int result = 0;

        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();

        c1.setTime(date1);
        c2.setTime(date2);

        result = c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH);
        int month = (c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR)) * 12;
        return Math.abs(month + result);

    }

    public static Integer  getYear(Date date){
        if(date == null){
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int year = calendar.get(Calendar.YEAR);//获取年份
        return year;
    }
}
